package com.example.myapplication.ui;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;

import com.android.volley.RequestQueue;
import com.example.myapplication.NewsInfo.NewsInfo;
import com.example.myapplication.R;
import com.example.myapplication.utils.Config;
import com.example.myapplication.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

public class NewsFragment extends Fragment {

    private ListView listView;
    private RequestQueue queue;
    private List<NewsInfo.NewsInfoDetail> newsList = new ArrayList<NewsInfo.NewsInfoDetail>();

    MyNewsAdapter adapter;

    public NewsFragment(List<NewsInfo.NewsInfoDetail> newsList,RequestQueue queue) {
        this.newsList = newsList;
        this.queue = queue;
    }

    public List<NewsInfo.NewsInfoDetail> getNewsList() {
        return newsList;
    }

    public void setNewsList(List<NewsInfo.NewsInfoDetail> newsList) {
        this.newsList = newsList;
        //adapter = new MyNewsAdapter(newsList,getContext(),queue);
        //listView.setAdapter(adapter);
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }
    }

    @Nullable

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i("aaaaa","aaaaaaaaaaaaaa");
        View v = inflater.inflate(R.layout.news_fragment,container,false);

        listView = v.findViewById(R.id.news_list);
         adapter = new MyNewsAdapter(newsList,getContext(),queue);
        listView.setAdapter(adapter);
        return v;
    }

    class MyNewsAdapter extends BaseAdapter{
        private List<NewsInfo.NewsInfoDetail> newsList = new ArrayList<NewsInfo.NewsInfoDetail>();
        private Context context;
        private RequestQueue queue;
        public MyNewsAdapter(List<NewsInfo.NewsInfoDetail> newsList, Context context ,RequestQueue queue) {
            this.newsList = newsList;
            this.context = context;
            this.queue = queue;
        }

        @Override
        public int getCount() {
            return newsList.size();
        }

        @Override
        public Object getItem(int i) {
            return newsList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = LayoutInflater.from(context).inflate(R.layout.news_list_item,viewGroup,false);
            ImageView img = v.findViewById(R.id.news_list_item_img);
            TextView title = v.findViewById(R.id.news_list_item_title);
            TextView content = v.findViewById(R.id.news_list_item_content);
            TextView time = v.findViewById(R.id.news_list_item_time);
            TextView like = v.findViewById(R.id.news_list_item_lickNum);
            TextView views = v.findViewById(R.id.news_list_item_viewsNum);

            ImageUtils.Loadimg(img, Config.URL + newsList.get(i).getImgUrl(),queue);
            title.setText(newsList.get(i).getTitle());
            content.setText("内容:"+newsList.get(i).getContent());
            time.setText("时间:"+newsList.get(i).getCreateTime());
            like.setText("收藏数:"+newsList.get(i).getLikeNumber().toString());
            views.setText("观看数:"+newsList.get(i).getViewsNumber().toString());
            return v;
        }
    }
}
